home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL07.TXT < prev    next >
Text File  |  1986-04-05  |  6KB  |  191 lines

  1. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 28
  2.  
  3.  
  4. TURBO-LESSON 7:   REPEAT STATEMENT                             
  5.  
  6. OBJECTIVES - In lesson 7 you should learn about:
  7.  
  8. 1.  CHARacter variables
  9. 2.  BOOLEAN variables
  10. 3.  REPEAT statement
  11.  
  12. 1.  CHARacter variables.
  13.  
  14. The reserved word, CHAR, is used to declare a variable of 
  15. character type.  A variable of type CHAR can be used to refer to 
  16. or store a single character.  
  17.  
  18. VAR
  19.    Alpha : CHAR;
  20.  
  21. Alpha is declared to be a variable which can be used to store any 
  22. of the characters in the character set.  This includes the upper 
  23. and lower case alphabet, the digits, 0 to 9, special characters 
  24. such as #, $, %, *, and the rest of the 256 characters in the 
  25. PC's character set.
  26.  
  27. ##### DO:
  28.  
  29. Examine PROG7.  
  30.  
  31. A variable named Response, of type CHAR, is used to store the 
  32. character entered in response to a multiple choice question.  
  33.  
  34. ##### DO:
  35.  
  36. Run the program several times, entering wrong responses, and the 
  37. correct response, D.  Also try lower case d.  
  38.  
  39. ##### DO:
  40.  
  41. Study the first IF statement in PROG7.  
  42.  
  43. IF (Response = 'D') OR (Response = 'd')
  44.     THEN . . .
  45.  
  46. The character 'D' must be enclosed in single quotes in the 
  47. program.  Note that you enter the character as input data without 
  48. quotes when running the program.  
  49.  
  50. Notice the compound condition using OR to combine two simple 
  51. conditions.  This condition will be true if either  or both of 
  52. the simple conditions are true.  The correct response, D, is 
  53. checked in both upper and lower case to make responding easier.
  54.  
  55. ##### DO:
  56.  
  57. Modify the IF statement to accept A, B, or C as the right response.  
  58. Assume that the correct answer is A or B or C.  (Ignore lower case 
  59. responses to keep the statement short.) 
  60. î
  61. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 29
  62.  
  63.  
  64. 2.  BOOLEAN variables.
  65.  
  66. BOOLEAN variables can have only two values, TRUE or FALSE.   The 
  67. value of a condition, TRUE or FALSE, may be stored in a BOOLEAN 
  68. variable for later use.
  69.  
  70. VAR
  71.   Correct_Response : Boolean;
  72.  
  73. In PROG7, the Boolean variable, Correct_Response, is used to 
  74. store the truth value (TRUE or FALSE) of the condition in the 
  75. first IF statement.  If the correct character, 'D' or 'd', is 
  76. entered, TRUE is stored in Correct_Response.  If anything else is 
  77. entered, FALSE is stored.  
  78.  
  79. Actually, TRUE and FALSE are stored as 1 and 0 to take up less 
  80. space, but you can always view a BOOLEAN variable as having a 
  81. value of TRUE or FALSE.
  82.  
  83. ##### DO:
  84.  
  85. Identify the condition in the second IF statement in PROG7.  
  86.  
  87. Since BOOLEAN variables can only have two values, TRUE or FALSE, 
  88. and conditions always evaluate to the same two values, a BOOLEAN 
  89. variable may be substituted for a condition.  
  90.  
  91. If would be permissible, but unnecessary to write the IF 
  92. statement: 
  93.  
  94. IF Correct_Response = TRUE
  95.    THEN . . .
  96.  
  97. ##### DO:
  98.  
  99. Modify the IF statement as indicated above and run the program to 
  100. verify that the IF still works exactly as before.
  101.  
  102. There is another way to assign the correct value to the variable, 
  103. Correct_Response.  
  104.  
  105. ##### DO:
  106.  
  107. Replace the FIRST IF statement in PROG7 with the following 
  108. statement:
  109.  
  110. Correct_Response := (Response = 'D') OR (Response = 'd');
  111.  
  112. Run the program.
  113.  
  114. How does the program change when you run it?  (If it doesn't do 
  115. exactly as before, maybe you typed it wrong, or replaced the 
  116. second IF instead of the first?)  
  117.  
  118. Since the condition on the right of the := must be evaluated by 
  119. the computer and assigned a value of TRUE or FALSE, this value 
  120. can be stored directly in a BOOLEAN variable without using the IF 
  121. statement.
  122. î
  123. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 30
  124.  
  125.  
  126. 3.  REPEAT Statement.
  127.  
  128. In a previous lesson, you learned that there are three ways to 
  129. sequence the execution of statements in PASCAL: SIMPLE SEQUENCE, 
  130. SELECTION STRUCTURES, and REPETITION STRUCTURES.
  131.  
  132. One of the statements used for REPETITION is REPEAT . . UNTIL.  
  133. The form of the REPEAT statement is:
  134.  
  135. REPEAT
  136.   Statement 1;
  137.   Statement 2;
  138.     .
  139.     .
  140.     .
  141.   Statement n
  142. UNTIL condition;
  143.  
  144. Statements 1, 2, . . . , n will be executed repeatedly until the 
  145. condition becomes true.  This implies that the condition is 
  146. checking something that can be changed by the statements 1 to n.  
  147. If this is not so, the statements will be repeated forever!
  148.  
  149. In PROG7, you are prompted to respond to the multiple choice 
  150. question.  A REPEAT statement controls the block of statements 
  151. which prompt for a response, and then check the response.  The 
  152. block of statements will be repeated until the UNTIL condition is 
  153. true.
  154.  
  155. ##### DO:
  156.  
  157. Change the condition in the UNTIL in PROG7 to:
  158.  
  159. UNTIL 'A' = 'B';
  160.  
  161. Run the program.  
  162.  
  163. Does the program correctly identify a correct response?  What 
  164. happens then.  
  165.  
  166. (Use ctrl-c or ctrl-Scroll-Lock to stop the program.) 
  167.  
  168. ##### DO:
  169.  
  170. Change the condition again to:
  171.  
  172. UNTIL 'A' = 'A';
  173.  
  174. How many times are you prompted for a response?  
  175.  
  176. Notice that the statements in a REPEAT structure are ALWAYS 
  177. executed at least once.  Even if the UNTIL condition is TRUE 
  178. before entering the REPEAT, the condition is not checked until 
  179. the end of the statements in the REPEAT block.  
  180. î
  181. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 31
  182.  
  183.  
  184. ##### DO:
  185.  
  186. Try to find another way to terminate the REPEAT without using the 
  187. BOOLEAN variable, Correct_Response, in the UNTIL condition.
  188.  
  189. Hint: Check the statement which assigns a value of TRUE or FALSE 
  190. to Correct_Response.
  191. î